<?php

/**
 * Effettua il migrate su tabelle
 * @package    Adapter
 * @category Migrate
 * @author    Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link    http://code.google.com/p/syrius/
 *
 */

namespace Syrius\Adapter;

class Migrate {
    /**
     * Effettua il migrate su una tabella esistente
     * @param \Syrius\Model\Model $model
     * @return \Syrius\Query\Table_Alter
     */
    public function update($model) {
        $table = new \Syrius\Query\Table_Alter ($model->source());
        $transaction = new Transaction ();
        $transaction->start();
        $stat = new Stat ();
        $oldCols = $stat->getColumnsForTable($model->source());

        $addCols = array();
        $updateCols = array();
        $removeCols = array();

        $fields = $model->fields();
        $newCols = $model->fields();
        $keyCols = array_keys($newCols);
        // ciclo le colonne esistenti
        foreach ($oldCols as $v) {
            if (isset ($newCols [$v->Field]) && $newCols [$v->Field]->isIndex()) {
                $newKeys [] = $v->Field;
            }
            if (array_search($v->Field, $keyCols) === false) {
                // la colonna esiste non c'è nella lista delle nuove
                $table->delColumn($v);
            } else {
                $table->updateColumn($v, $newCols [$v->Field]);
                unset ($newCols [$v->Field]);
            }
        }
        $addCols = $newCols;
        foreach ($addCols as $k => $v) {
            $table->addColumn($k, $v);
        }

        // gestione delle keys
        $oldKeys = $stat->getKeysForTable($model->source());
        foreach ($newKeys as $k => $v) {
            $i = 0;
            while ($i < count($oldKeys)) {
                if ($v == $oldKeys [$i]->Column_name) {
                    break;
                }
                $i++;
            }
            if ($i < count($oldKeys)) {
                // se la chiave c'è allora devo solo aggiornarla
                unset ($newKeys [$i]);
            } else {
                // se la chiave non c'è nella lista delle nuove la cancello
                $table->addKey($fields [$v]);
                unset ($newKeys [$i]);
            }
        }
        return $table;
    }

    /**
     * Creazione di una nuova tabella
     * @param \Syrius\Model\Model $model
     * @return \Syrius\Query\Table_Create
     */
    public function create($model) {
        $table = new \Syrius\Query\Table_Create ($model->source());
        $fields = $model->fields();
        foreach ($fields as $k => $v) {

            if (!is_object($v))
                continue;
            $table->addColumn($k, $v);

            if ($v->getAttribute("primary")) {
                // devo creare una chiave primaria
                $primary = new \Syrius\Keys\Primary ($k);
                $table->addKey($primary);
            }
        }
        return $table;
    }

}

/**
 * Effettua il migrate sulle view
 * @package    Adapter
 * @category Migrate
 * @author    Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link    http://code.google.com/p/syrius/
 *
 */
class Migrate_View extends Migrate {
    /**
     * @see Syrius\Adapter.Migrate::create($model)
     */
    public function create($model) {
        $table = new \Syrius\Query\View_Create ($model->source(), $model->getSelect());
        return $table;
    }

    /**
     * @see Syrius\Adapter.Migrate::update($model)
     */
    public function update($model) {
        $table = new \Syrius\Query\View_Alter ($model->source(), $model->getSelect());
        return $table;
    }

}

/**
 * Effettua il migrate sulle funzioni,
 * le funzioni vengono sempre create nuovamente utilizzato la condizione
 * IF NOT EXISTS
 * @package    Adapter
 * @category Migrate
 * @author    Domenico Biancardi <domenico.biancardi@gmail.com>
 * @link    http://code.google.com/p/syrius/
 */
class Migrate_Function extends Migrate {
    /**
     * @see Syrius\Adapter.Migrate::create($model)
     */
    public function create($model) {
        $function = new \Syrius\Query\FunctionM_Create ($model->getName(), $model->getBody());
        return $function;
    }

}

?>
